package quan.dong.pricealert;

import java.util.ArrayList;

import quan.dong.adapter.ListviewAdapter;
import quan.dong.common.Common;
import quan.dong.common.Constant;
import quan.dong.handle.GetPriceAlertList;
import quan.dong.object.PriceAlert;
import android.app.AlertDialog;
import android.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

public class AlertFragment extends Fragment {

	public AlertFragment() {
	}

	ListView lvPriceAlert;
	ArrayList<PriceAlert> priceAlerts;

	final Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case Constant.HANDLER_GET_PRICE_ALERT_LIST:
				priceAlerts = (ArrayList<PriceAlert>) msg.obj;
				if (priceAlerts.size() > 0) {
					UpdateListView();
				} else {
					Toast.makeText(getActivity(), "khong co phan tu nao",
							Toast.LENGTH_LONG).show();
				}
				break;

			default:
				break;
			}
			super.handleMessage(msg);
		}
	};

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_alert, container,
				false);
		priceAlerts = new ArrayList<PriceAlert>();
		lvPriceAlert = (ListView) rootView.findViewById(R.id.listView1);
		GetPriceAlertList getPriceAlertList = new GetPriceAlertList(
				Common.GetImeiNumber(getActivity()), handler, true,
				"GetPriceAlertList");
		getPriceAlertList.Start();

		return rootView;
	}

	private void UpdateListView() {
		ListviewAdapter adapter = new ListviewAdapter(getActivity(),
				R.layout.listview_item, priceAlerts);
		lvPriceAlert.setAdapter(adapter);
		// listViewItems.setOnItemClickListener(new
		// OnItemClickListenerListViewItem());
	}
}
